
package ubi3d.Data;

 /*
 * Creates a datasource for the co-ordinate data in the 3d world

   Copyright 2009 BBC Research, David Lewis, <david.lewis4@bbc.co.uk>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.


 */

public class DataSource {
    DBThread2 dtb;  //Database
    LiveThread lvt; //Live ubisense system
    CamThread cmt;  //Camera database
    boolean local;  //Source is non-network
    boolean cam;    //Use camera database
    int firstTC = -1;   //Start timecode
    int currentTc= 0;   //Timecode

    /**
     * Constructor for a datasource which hides different threads for live or database data.
     * @param local True if database
     * @param address IP address or database url
     * @param table table name if a database
     * @param cam True to try and use a camera position table
     * @return
     */
    public DataSource main(Boolean local, String address, String table, Boolean cam) {
        DataSource ds = new DataSource();

        if (local) {                            //Start a database reading thread

            ds.dtb = new DBThread2(address, ds, table);
            
            ds.local =local;
            if(cam){    //Start a camera db reading thread
            ds.cmt = new CamThread(address, ds, table);
            ds.cmt.start();
            }

            ds.dtb.start();
        } else {                                //Start a web service recieving thread
            if(address.equalsIgnoreCase("live")){
                ds.lvt = new LiveThread(address, ds, Integer.valueOf(table));
            ds.lvt.start();
            System.out.println("Started");
            ds.local =local;
            }

        }
        return ds;
    }

    /**
     * Interrupt and stop all running data threads
     */
    public void stop(){
    if (dtb!=null){ dtb.interrupt();}
    if (lvt!=null){ lvt.interrupt();}
    if (cmt!=null){ cmt.interrupt();}
    }

    /**
     * Pass on to the database thread that we want to skip ahead
     * @param skipTime Time in ms to skip
     */
    public void skip(int skipTime){
        if(local){
            dtb.skip(skipTime);
            
        }else{

        }
    }


}
